package com.gzjh_group.photoalbum;

import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;

import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory;
import com.bumptech.glide.load.engine.cache.LruResourceCache;
import com.bumptech.glide.load.engine.cache.MemorySizeCalculator;
import com.bumptech.glide.module.AppGlideModule;
import com.bumptech.glide.request.RequestOptions;

@GlideModule
public class MyAppGlideModule extends AppGlideModule {

    @Override
    public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) {
        // 设置内存缓存 setMemoryCacheScreens默认缓存多少张图片
        MemorySizeCalculator calculator = new MemorySizeCalculator.Builder(context).setMemoryCacheScreens(2).build();
        builder.setMemoryCache(new LruResourceCache(calculator.getMemoryCacheSize()));

        // 也可以直接覆盖缓存大小
        //int memoryCacheSizeBytes = 1024 * 1024 * 20; // 20mb
        //builder.setMemoryCache(new LruResourceCache(memoryCacheSizeBytes));

        // 应用程序甚至可以提供它们自己的实现MemoryCache：
        //builder.setMemoryCache(new YourAppMemoryCacheImpl());

        builder.setDiskCache(new InternalCacheDiskCacheFactory(context));

        // 虽然RequestOptions通常是按照每个请求指定的，但您也可以应用默认设置，RequestOptions该设置将应用于您在应用程序中启动的每次加载，方法如下AppGlideModule：
        builder.setDefaultRequestOptions(new RequestOptions().format(DecodeFormat.PREFER_RGB_565).disallowHardwareConfig());

        builder.setLogLevel(Log.DEBUG);
    }
}
